//
//  STMemorailController.swift
//  SlowTrajectory
//
//  Created by iOS-Czz on 2023/10/23.
//

import UIKit
import WMZDialog
class STMemorailController: STProjectBaseController {
   
    @IBOutlet var newButton: UIButton!
    @IBOutlet var latelyButton: UIButton!
    @IBOutlet var tableView: UITableView!
    @IBOutlet var allButton: UIButton!
    lazy var viewModel: STMemorailViewModel = {
        return STMemorailViewModel()
    }() 
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        clickAllAction("")
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        self.wx_navigationBar.isHidden = true
        setTab()
        // Do any additional setup after loading the view.
    }
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        self.newButton.addDashedBorder(color: .black, lineWidth: 1, cornerRadius: 15)
        self.allButton.addDashedBorder(color: .black, lineWidth: 1, cornerRadius: 15)
        self.latelyButton.addDashedBorder(color: .black, lineWidth: 1, cornerRadius: 15)
    }
    
    @IBAction func newAnniversaryAction(_ sender: Any) {
        self.navigationController?.pushViewController(STAddAndEditMemorailController(), animated: true)
    }
    
    @IBAction func clickAllAction(_ sender: Any) {
        self.allButton.isSelected = true
        self.latelyButton.isSelected = false
        changeButtonStatus()
        loadData(0)
    }
    @IBAction func clickLatelyAction(_ sender: Any) {
        self.allButton.isSelected = false
        self.latelyButton.isSelected = true
        changeButtonStatus()
        loadData(1)
    }
    func changeButtonStatus(){
        if self.allButton.isSelected {
            self.allButton.backgroundColor = #colorLiteral(red: 0.911305666, green: 0.8569386601, blue: 0.3978728056, alpha: 1)
            self.latelyButton.backgroundColor = #colorLiteral(red: 0.9119143486, green: 0.8772469163, blue: 0.6217344999, alpha: 1)
            self.allButton.setTitleColor(UIColor(red: 26/255, green: 26/255, blue: 26/255, alpha: 1), for: .normal)
            self.latelyButton.setTitleColor(UIColor(red: 133/255, green: 128/255, blue: 97/255, alpha: 1), for: .normal)
        }else{
            self.allButton.backgroundColor = #colorLiteral(red: 0.9119143486, green: 0.8772469163, blue: 0.6217344999, alpha: 1)
            self.latelyButton.backgroundColor = #colorLiteral(red: 0.911305666, green: 0.8569386601, blue: 0.3978728056, alpha: 1)
            self.latelyButton.setTitleColor(UIColor(red: 26/255, green: 26/255, blue: 26/255, alpha: 1), for: .normal)
            self.allButton.setTitleColor(UIColor(red: 133/255, green: 128/255, blue: 97/255,  alpha: 1), for: .normal)
        }
    }
    func setTab(){
        self.tableView.dataSource = self
        self.tableView.delegate = self
        self.tableView.register(UINib(nibName: "STMemorailTableCell", bundle: .main), forCellReuseIdentifier: "STMemorailTableCell")
    }
    func loadData(_ type:Int){
        self.viewModel.getPageData(type) { models in
            print(self.viewModel.lately)
            self.tableView.reloadData()
        }
        
    }
}
extension STMemorailController:UITableViewDataSource,UITableViewDelegate{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.viewModel.dataSource.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell:STMemorailTableCell = tableView.dequeueReusableCell(withIdentifier: "STMemorailTableCell") as! STMemorailTableCell
        let model = self.viewModel.dataSource[indexPath.row]
        cell.model = model
        return cell
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let detailsVc = STMemorailDetailsController()
        detailsVc.model = self.viewModel.dataSource[indexPath.row]
        self.navigationController?.pushViewController(detailsVc, animated: true)
    }
}
